/**
 * Created by Eric on 14-1-5.
 */
define(function (require, exports) {

    require('css/easyui.css');
    require('css/demo.css');
    require('css/icon.css');
    require('css/page/app.css');

    var jQuery = require('lib/jquery');
    require('lib/easyui')(jQuery);
    require('lib/plugins/jquery.element')(jQuery);
    require('lib/plugins/jquery.ajaxform')(jQuery);

    var template = require('data/app');
    var $ = jQuery;


    var tableOptions = {
        columns: [template.fields],
        toolbar: [
            {
                iconCls: 'icon-add',
                handler: function () {
                    $('#addPanel').dialog('open');
                }
            },
            {
                iconCls: 'icon-edit',
                handler: function () {
                    //  var pager =  $('#testTab').datagrid('getPager');
                    console.log(pager);
                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                handler: function () {
                    alert('remove')
                }
            }
        ]};
    var fromOptions = {
        columns: [template.fields]
    };
    var dialogOptions = {
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $('#addForm').ajaxSubmit(fromOptions);
                    $('#addPanel').dialog('close'); // close the dialog
                    $('#testTab').datagrid('reload'); // reload the user data
                }
            },
            {
                text: '关闭',
                handler: function () {
                    $('#addPanel').dialog('close'); // close the dialog
                    $('#testTab').datagrid('reload'); // reload the user data
                }
            }
        ]
    };

    $.extend(tableOptions, template.views_table);
    $.extend(fromOptions, template.view_from);
    $.extend(dialogOptions, template.view_dialog);

    $.extend($.fn.pagination.defaults, {afterPageText: '页 - 共{pages}页', beforePageText: '第', displayMsg: '显示从 {from} 到 {to} 页面总数 {total} 的记录', total: 20})


    exports.init = function () {
        $(document).ready(function () {
            console.log("start ready this page!!!!!");
            $('#addPanel').dialog(dialogOptions);
            $('#testTab').datagrid(tableOptions);
            var el = $.element;
            $.each(template.fields, function (i, fieldData) {
                console.log(fieldData);
                el.div({ className: 'fitem' },
                    el.label({  htmlFor: fieldData.field + '-input' },
                        fieldData.title + ' :'
                    ),
                    el.input({id: fieldData.field + '-input', name: fieldData.field, type: 'text', required: 'true'})

                ).appendTo($('#addForm'));
            });
            var form = $('#addForm').appendTo("#insertForm");
        });
    }
});